home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Chess / Fly_Chess.jar / ChessGUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-08-23  |  4.9 KB  |  261 lines

  1. import java.io.IOException;
  2. import javax.microedition.lcdui.Canvas;
  3. import javax.microedition.lcdui.Graphics;
  4. import javax.microedition.lcdui.Image;
  5.  
  6. public class ChessGUI {
  7.    public static final int MAX_COMMANDS = 2;
  8.    private static final int LINE_CORR = 1;
  9.    private static final int BACKGROUND = 12439485;
  10.    private static final int STRINGHEIGHT = 24;
  11.    private Canvas canvas;
  12.    private Image chessimg;
  13.    private Graphics chessgraph;
  14.    private Image[] pieces;
  15.    private String[] commands;
  16.  
  17.    public ChessGUI(Canvas var1) {
  18.       this.canvas = var1;
  19.       this.initializeImage("/xboard.png");
  20.       this.commands = new String[3];
  21.  
  22.       for(int var2 = 1; var2 <= 2; ++var2) {
  23.          this.commands[var2] = "";
  24.       }
  25.  
  26.    }
  27.  
  28.    private void createSubImages(Image var1) {
  29.       this.pieces = new Image[28];
  30.       int var3 = var1.getWidth() / 8;
  31.       int var4 = var1.getHeight() / 8;
  32.  
  33.       for(int var6 = 0; var6 < 28; ++var6) {
  34.          byte var2 = 11;
  35.          switch (var6) {
  36.             case 0:
  37.                var2 = 26;
  38.                break;
  39.             case 1:
  40.                var2 = 17;
  41.                break;
  42.             case 2:
  43.                var2 = 88;
  44.                break;
  45.             case 3:
  46.                var2 = 28;
  47.                break;
  48.             case 4:
  49.                var2 = 68;
  50.                break;
  51.             case 5:
  52.                var2 = 48;
  53.                break;
  54.             case 6:
  55.                var2 = 55;
  56.                break;
  57.             case 7:
  58.                var2 = 16;
  59.                break;
  60.             case 8:
  61.                var2 = 27;
  62.                break;
  63.             case 9:
  64.                var2 = 18;
  65.                break;
  66.             case 10:
  67.                var2 = 78;
  68.                break;
  69.             case 11:
  70.                var2 = 38;
  71.                break;
  72.             case 12:
  73.                var2 = 45;
  74.                break;
  75.             case 13:
  76.                var2 = 58;
  77.                break;
  78.             case 14:
  79.                var2 = 13;
  80.                break;
  81.             case 15:
  82.                var2 = 22;
  83.                break;
  84.             case 16:
  85.                var2 = 11;
  86.                break;
  87.             case 17:
  88.                var2 = 71;
  89.                break;
  90.             case 18:
  91.                var2 = 31;
  92.                break;
  93.             case 19:
  94.                var2 = 44;
  95.                break;
  96.             case 20:
  97.                var2 = 51;
  98.                break;
  99.             case 21:
  100.                var2 = 23;
  101.                break;
  102.             case 22:
  103.                var2 = 12;
  104.                break;
  105.             case 23:
  106.                var2 = 81;
  107.                break;
  108.             case 24:
  109.                var2 = 21;
  110.                break;
  111.             case 25:
  112.                var2 = 61;
  113.                break;
  114.             case 26:
  115.                var2 = 41;
  116.                break;
  117.             case 27:
  118.                var2 = 54;
  119.          }
  120.  
  121.          this.pieces[var6] = Image.createImage(var3 - 2, var4 - 2);
  122.          Graphics var5 = this.pieces[var6].getGraphics();
  123.          var5.drawImage(var1, -1 - this.getXPos(var2), -1 - this.getYPos(var2), 20);
  124.       }
  125.  
  126.    }
  127.  
  128.    public void drawChessGraphics(Graphics var1) {
  129.       var1.drawImage(this.chessimg, this.canvas.getWidth() - this.chessimg.getWidth(), 0, 20);
  130.    }
  131.  
  132.    public void drawCommands(Graphics var1) {
  133.       int var2 = this.chessimg.getHeight() / 8;
  134.       var1.setColor(12439485);
  135.       var1.fillRect(0, 0, this.canvas.getWidth() - this.chessimg.getWidth(), this.chessimg.getHeight());
  136.       var1.setColor(0, 0, 0);
  137.  
  138.       for(int var3 = 0; var3 < 4; ++var3) {
  139.          if (this.commands[1].length() > var3) {
  140.             var1.drawChar(this.commands[1].charAt(var3), 5, var3 * var2, 20);
  141.          }
  142.       }
  143.  
  144.       var1.drawLine(0, 4 * var2 - 2, this.canvas.getWidth() - this.chessimg.getWidth(), 4 * var2 - 2);
  145.       var1.drawLine(0, 4 * var2 - 1, this.canvas.getWidth() - this.chessimg.getWidth(), 4 * var2 - 1);
  146.  
  147.       for(int var4 = 0; var4 < 4; ++var4) {
  148.          if (this.commands[2].length() > var4) {
  149.             var1.drawChar(this.commands[2].charAt(var4), 5, (var4 + 4) * var2, 20);
  150.          }
  151.       }
  152.  
  153.       var1.drawLine(0, this.chessimg.getHeight() - 2, this.canvas.getWidth() - this.chessimg.getWidth(), this.chessimg.getHeight() - 2);
  154.       var1.drawLine(0, this.chessimg.getHeight() - 1, this.canvas.getWidth() - this.chessimg.getWidth(), this.chessimg.getHeight() - 1);
  155.    }
  156.  
  157.    public boolean drawInformation(Graphics var1, int var2, String var3) {
  158.       var1.setColor(12439485);
  159.       var1.fillRect(0, (var2 - 1) * 24 + this.chessimg.getHeight(), this.canvas.getWidth(), 24);
  160.       var1.setColor(0, 0, 0);
  161.       var1.drawString(var3, 5, (var2 - 1) * 24 + this.chessimg.getHeight(), 20);
  162.       return true;
  163.    }
  164.  
  165.    public void drawPiece(Graphics var1, int var2, int var3, int var4) {
  166.       int var5 = var4 / 10 + var4 % 10;
  167.       byte var6;
  168.       if (var5 % 2 == 0) {
  169.          var6 = 0;
  170.       } else {
  171.          var6 = 1;
  172.       }
  173.  
  174.       var5 = var3 * 14 + var6 * 7 + var2;
  175.       var1.drawImage(this.pieces[var5], this.getXPos(var4) + 1, this.getYPos(var4) + 1, 20);
  176.    }
  177.  
  178.    public void drawPiece(Graphics var1, int var2, int var3, int var4, int var5, int var6) {
  179.       int var7 = var3 * 14 + var4 * 7 + var2;
  180.       var1.drawImage(this.pieces[var7], var5, var6, 20);
  181.    }
  182.  
  183.    public Graphics getChessGraphics() {
  184.       return this.chessgraph;
  185.    }
  186.  
  187.    public int getCommand(int var1, int var2) {
  188.       int var3 = this.chessimg.getHeight() / 8;
  189.       if (var1 < this.canvas.getWidth() - this.chessimg.getWidth()) {
  190.          if (var2 < 4 * var3) {
  191.             return 1;
  192.          } else {
  193.             return var2 < 8 * var3 ? 2 : 0;
  194.          }
  195.       } else {
  196.          return 0;
  197.       }
  198.    }
  199.  
  200.    public String getCommandName(int var1) {
  201.       return this.commands[var1];
  202.    }
  203.  
  204.    public int getFieldCode(int var1, int var2) {
  205.       int var3 = var1 - (this.canvas.getWidth() - this.chessimg.getWidth());
  206.       int var4 = (var3 / (this.chessimg.getWidth() / 8) + 1) * 10;
  207.       var4 += (this.chessimg.getHeight() - var2) / (this.chessimg.getHeight() / 8) + 1;
  208.       return var4;
  209.    }
  210.  
  211.    private int getXPos(int var1) {
  212.       return (var1 / 10 - 1) * (this.chessimg.getWidth() / 8);
  213.    }
  214.  
  215.    private int getYPos(int var1) {
  216.       return this.chessimg.getHeight() - var1 % 10 * this.chessimg.getHeight() / 8;
  217.    }
  218.  
  219.    public boolean inChessBoard(int var1, int var2) {
  220.       return var1 > this.canvas.getWidth() - this.chessimg.getWidth() && var2 < this.chessimg.getHeight();
  221.    }
  222.  
  223.    public boolean initializeImage(String var1) {
  224.       try {
  225.          this.chessimg = Image.createImage("/xboard.png");
  226.          this.createSubImages(this.chessimg);
  227.          Image var2 = this.chessimg;
  228.          this.chessimg = Image.createImage(var2.getWidth(), var2.getHeight());
  229.          this.chessgraph = this.chessimg.getGraphics();
  230.          this.chessgraph.drawImage(var2, 0, 0, 20);
  231.          return true;
  232.       } catch (IOException var3) {
  233.          CU.shout("initializeImage error" + ((Throwable)var3).toString());
  234.          return false;
  235.       }
  236.    }
  237.  
  238.    public boolean registerCommand(int var1, String var2) {
  239.       if (var1 >= 1 && var1 <= 2) {
  240.          this.commands[var1] = var2;
  241.          return true;
  242.       } else {
  243.          return false;
  244.       }
  245.    }
  246.  
  247.    public void selectField(Graphics var1, int var2, int var3, int var4, int var5, boolean var6) {
  248.       int var7 = var1.getColor();
  249.       var1.setColor(var3, var4, var5);
  250.       int var8 = this.getXPos(var2);
  251.       int var9 = this.getYPos(var2);
  252.       if (var6) {
  253.          var1.fillArc(var8 + 3, var9 + 1, this.chessimg.getWidth() / 8 - 5, this.chessimg.getHeight() / 8 - 4, 0, 360);
  254.       } else {
  255.          var1.drawArc(var8 + 3, var9 + 1, this.chessimg.getWidth() / 8 - 5, this.chessimg.getHeight() / 8 - 4, 0, 360);
  256.       }
  257.  
  258.       var1.setColor(var7);
  259.    }
  260. }
  261.